"""
清理 Mock 测试用户
删除所有 wechat_openid 以 'mock_' 开头的测试用户
"""
import sys
import os

# 添加项目根目录到 Python 路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.core.database import SessionLocal
from app.models.user import User
from app.core.logger import logger


def clean_mock_users():
    """清理 Mock 测试用户"""
    db = SessionLocal()
    
    try:
        logger.info("🔍 开始查找 Mock 测试用户...")
        
        # 查找所有 Mock 用户
        mock_users = db.query(User).filter(
            User.wechat_openid.like('mock_%')
        ).all()
        
        if not mock_users:
            logger.info("✅ 没有找到 Mock 测试用户")
            return
        
        logger.info(f"📋 找到 {len(mock_users)} 个 Mock 测试用户:")
        
        for user in mock_users:
            logger.info(f"  - ID: {user.id}")
            logger.info(f"    昵称: {user.nick_name}")
            logger.info(f"    OpenID: {user.wechat_openid}")
            logger.info(f"    创建时间: {user.created_at}")
        
        # 确认删除
        print("\n" + "="*60)
        print("⚠️  警告：即将删除以上测试用户！")
        print("="*60)
        confirm = input("确认删除？(输入 'yes' 确认): ")
        
        if confirm.lower() != 'yes':
            logger.warning("❌ 已取消删除操作")
            return
        
        # 删除用户
        delete_count = 0
        for user in mock_users:
            try:
                db.delete(user)
                delete_count += 1
                logger.info(f"✅ 已删除用户: {user.nick_name} ({user.id})")
            except Exception as e:
                logger.error(f"❌ 删除用户失败: {user.id}, 错误: {e}")
        
        db.commit()
        logger.success(f"\n🎉 成功删除 {delete_count} 个 Mock 测试用户！")
        
    except Exception as e:
        logger.error(f"❌ 清理失败: {e}")
        db.rollback()
        raise
    finally:
        db.close()


if __name__ == "__main__":
    logger.info("="*60)
    logger.info("🧹 Mock 测试用户清理工具")
    logger.info("="*60)
    
    clean_mock_users()
    
    logger.info("\n✅ 清理完成！")

